package handlers

import (
	"github.com/gin-gonic/gin"
	"strconv"
	"work-flow-hub/moderls"
)

func List(c *gin.Context) {

	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	size, _ := strconv.Atoi(c.DefaultQuery("size", "5"))

	offset := (page - 1) * size

	var work []moderls.Work

	err := moderls.DB.Offset(offset).Limit(size).Find(&work).Debug().Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "查询失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
	})
	return
}

func Fail(c *gin.Context) {
	id := c.Query("id")
	var work moderls.Work
	err := moderls.DB.Where("id=?", id).First(&work).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "收藏失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 500,
		"msg":  "收藏成功",
	})
	return
}
